
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var userUrl = require("url/user");
var circleUrl = require("url/circle");
var communityUrl = require("url/community");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var imHelper = require("helper/im");
var openHelper = require("helper/open");
var friendHelper = require("helper/friend");
var barcodeHelper = require("helper/barcode");
var bizzType = require("helper/bizzType");
var helperVote = require("helper/vote");
var userUtil = require("helper/user");

var listview = ui("ListView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据

var userInfoUrl = userUrl.url.base.personalV1;
var circleTopicUrl = circleUrl.url.topic.listTopic4Her;//她发布的圈子动态
var communityMessageUrl = communityUrl.url.message.list4HerInCommunity;//分页查询她在一个社区发布的动态

/**模板列表
source://view/user/usercenter/cell0.ui,
source://view/user/usercenter/her/cell.ui,
source://view/user/usercenter/circle/cell.ui,
source://view/user/usercenter/community/cell.ui
*/

var mainTemplate = 0,herTemplate = 1,circleTopicTemplate = 2,communityMessageTemplate = 3,noDataTemplate = 4;
var noData = [{"template":noDataTemplate,"labelContent":"没有圈子动态"}];
//上个页面转过来的参数
var userId = null;
var isBlack = false;
var isFriend = false;
var isMySelf = false;
//当前页码
var pageNum4Circle = 1;//圈子动态页码
var pageNum4Community = 1;//社区动态页码
var curTabIndex = 1;//当前显示的页签1:他的;2-社区;3-圈子
var curCommunityId = null;//当前选中的社区id
//是否最后一页

//andriod返回键
page.on("back", function() {
    app.closePage();
});

//从新增页面返回,刷新数据
page.on("result", function(data){
    if(data.isCircleTopicUpdate){
    	for (var i = 0; i < listdata.getCount(); i++) {
    		var oneData = listdata.getOne(i);
    		if (oneData.template == circleTopicTemplate && oneData.id==data.topicId) {
    			//如果是圈子动态，定位到对应的那条数据并更新【评论数量、点赞数量和是否已点赞】
    			//deviceone.print("定位："+oneData.id+",是否点赞："+data.isPraised);
    			oneData.commentCount = data.commentCount;
    			oneData.praiseCount = data.praiseCount;
    			oneData.isPraised = data.isPraised;
    			listdata.updateOne(i, oneData);
    			listview.refreshItems();
    			break;
    		}
    	}
    }
});

page.on("loaded", function(){
	userId = page.getData().userId;
	getUserInfo();
});

//聊天
ui("aLayoutChat").on("touch", function() {
	if(isFriend){
		imHelper.toChat(userId);
	}else{
		nf.toast("你们还不是好友！");
	}
});

//添加好友/删除好友
ui("aLayoutFriend").on("touch", function() {
	addFriend();//加为好友
});
var userBase = null;
var circleList = null;
var communityList = null;

function getUserInfo(){
	httpSilence.post(userInfoUrl,{id:userId},function(data){
		isBlack = data.isBlack;
		isFriend = data.isFriend;
		isMySelf = data.isMySelf;
		userBase = data.userBaseVo;
		userBase.isFriend= isFriend;
		circleList = data.circleList;
		communityList = data.communityList;
		userBase.honorLevel = "v"+data.userValueVo.honorLevel;//等级
		userBase.honorValue = "荣誉值:"+data.userValueVo.honorValue;//荣誉值
		reDrawFooter();//重新加载foot
		if(communityList!=null && communityList.length > 0){
			for (var i = 0; i < communityList.length; i++) {
				communityList[i].fontColor = "949EAAFF";
			}
			communityList[0].fontColor ="FF8040FF";//选中第一个
			curCommunityId = communityList[0].id;
		}
		if(userBase!=null){
			listdata.removeAll();
			userBase.template = mainTemplate;
			listdata.addOne(userBase);//插入一条数据【用户名称、头象】
			listview.refreshItems();//刷新item数据
			addCell4Her();//添加【我的】数据
		}
	});
}

page.on("user_fire_del_friend_sucess", function(data){
	isFriend = false;
	reDrawFooter();
});
//重新加载foot
function reDrawFooter(){
	if(isMySelf){
		ui("aLayoutFooter").visible = false;
	}
	if(isFriend){
		ui("aLayoutFriend").visible = false;
		ui("aLayoutChat").visible = true;//只有【聊天】
	}else{
		ui("aLayoutFriend").visible = true;//只有【加为好友】
		ui("aLayoutChat").visible = false;
	}
	isFriendMenu(isFriend);//更改cell的右上角菜单项
}
//加载【他的】页签
function addCell4Her(){
	if(listdata.getCount()>1){
		listdata.removeRange(1, listdata.getCount()-1);
	}
		var herData = {};
		herData.template = herTemplate;
		herData.userId = userId;
		herData.sex = userBase.sex;//性别
		herData.interests = userBase.interests;
		herData.industry = userBase.profession;
		herData.hometown = userBase.hometown;
		herData.delicacy = userBase.delicacys;
		herData.graduateSchool = userBase.graduateSchool;
		herData.entertainment = userBase.entertainment;
		herData.communityList = communityList;
		herData.circleList = circleList;
		
		herData.showInrerests = true;//显示爱好
		herData.showIndustry = true;
		herData.showHomeTown = true;
		herData.showDelicacy = true;
		herData.showGraduateSchool = true;
		herData.showEntertainment = true;
		herData.showJoinCommunity = true;//显示社区
		herData.showJoinCommunityCount = true;//显示社区数量
		herData.showCircle = true;//显示圈子
		herData.showShop = true;//显示店铺
		
		herData.showJoinCommunity2 = false;//显示社区【长】
		herData.showHerCircle = false;//显示-他的圈子label
		
		listdata.addOne(herData);
		listview.refreshItems();//刷新item数据
}

//查询--圈子动态--第一页
function getCircleTopic4FirstPage(){
	pageNum4Circle = 1;
	var param = {"creatorId" :userId, "page" : pageNum4Circle};
	httpSilence.post(circleTopicUrl, param, function(data) {
		if (data.topicPage) {
			default_topicList = data.topicPage.content;
			if(default_topicList.length>0){
				for (var i = 0; i < default_topicList.length; i++) {
					default_topicList[i].template = circleTopicTemplate;
				}
				listdata.addData(default_topicList);
			}else{
				noData = [{"template":noDataTemplate,"labelContent":"没有圈子动态"}];
				listdata.addData(noData);
			}
			
		}
		listview.refreshItems();
	});
}

//查询--圈子动态--下一页
function getCircleTopic4NextPage(){
	pageNum4Circle ++;
	var param = {"creatorId" :userId, "page" : pageNum4Circle};
	httpSilence.post(circleTopicUrl, param, function(data) {
		if (data.topicPage) {
			var topicList = data.topicPage.content;
			for (var i = 0; i < topicList.length; i++) {
				topicList[i].template = circleTopicTemplate;
			}
			listdata.addData(topicList);
		}
		listview.refreshItems();
	});
}

//查询--社区动态--第一页
function getCommunityMessage4FirstPage(){
	pageNum4Community = 1;
	var param = {"creatorId":userId, "communityId" :curCommunityId, "page" : pageNum4Community};
	httpSilence.post(communityMessageUrl, param, function(data) {
		//deviceone.print(JSON.stringify(data.messagePage));
		if (data.messagePage) {
			default_messageList = data.messagePage.content;
			if(default_messageList.length>0){
				for (var i = 0; i < default_messageList.length; i++) {
					default_messageList[i].template = communityMessageTemplate;
				}
				listdata.addData(default_messageList);
			}else{
				noData = [{"template":noDataTemplate,"labelContent":"没有在该社区发布动态"}];
				listdata.addData(noData);
			}
		}
		listview.refreshItems();
	});
}
//查询-社区动态--下一页
function getCommunityMessage4NextPage(){
	pageNum4Community ++;
	var param = {"creatorId":userId, "communityId" :curCommunityId, "page" : pageNum4Community};
	httpSilence.post(communityMessageUrl, param, function(data) {
		if (data.messagePage) {
			var messageList = data.messagePage.content;
			for (var i = 0; i < messageList.length; i++) {
				messageList[i].template = communityMessageTemplate;
			}
			listdata.addData(messageList);
		}
		listview.refreshItems();
	});
}
//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		if(curTabIndex==1){//他的
			
		}else if(curTabIndex == 2){//社区
			
		}else if(curTabIndex == 3){//圈子
			
		}
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		if(curTabIndex==1){//他的
			//loadCell4Her();
		}else if(curTabIndex == 2){//社区
			getCommunityMessage4NextPage();
		}else if(curTabIndex == 3){//圈子
			getCircleTopic4NextPage();
		}
		listview.rebound();//复位【一定要做】
	}
});

//点击【他的】页签
page.on("user_her_tab_touch",function(data){
	//nf.toast("her");
	curTabIndex = 1;//当前显示【他的】
	reDrawHerInfo();//重画【他的】ui
	if(listdata.getCount()>2){
		listdata.removeRange(2, listdata.getCount()-1);
		listview.refreshItems();
	}
});

//点击【社区】页签
var default_messageList = null;
page.on("user_community_tab_touch",function(data){
	//nf.toast("community");
	curTabIndex = 2;//当前显示【社区动态】
	reDrawHerInfo();//重画【他的】ui
	pageNum4Community = 1;
	if(listdata.getCount()>2){
		listdata.removeRange(2, listdata.getCount()-1);
	}
	if(default_messageList!=null){
		listdata.addData(default_messageList);
		listview.refreshItems();
	}else{
		getCommunityMessage4FirstPage();
	}
});
//点击【圈子】页签
var default_topicList = null;
page.on("user_circle_tab_touch",function(data){
	curTabIndex = 3;//当前显示【圈子动态】
	reDrawHerInfo();//重画【他的】ui
	pageNum4Circle = 1;
	if(listdata.getCount()>2){
		listdata.removeRange(2, listdata.getCount()-1);
	}
	if(default_topicList!=null){
		listdata.addData(default_topicList);
		listview.refreshItems();
	}else{
		getCircleTopic4FirstPage();
	}
	
});

//点击一个社区segment
page.on("community_seg_touch",function(communityId){
	//nf.toast("communityId:"+communityId);
	curCommunityId = communityId;
	setSelectedCommunity(communityId);//选中某个社区
	pageNum4Community = 1;
	if(listdata.getCount()>2){
		listdata.removeRange(2, listdata.getCount()-1);
	}
	getCommunityMessage4FirstPage();
});

//设置选中某个社区
function setSelectedCommunity(communityId){
	if(listdata.getCount()<2 || communityId==null){
		return;
	}
	//deviceone.print(communityId)
	if(communityList!=null && communityList.length>0){
		for (var i = 0; i < communityList.length; i++) {
			if (communityList[i].id==communityId) {
				communityList[i].fontColor = "FF8040FF";//选中
			}else{
				communityList[i].fontColor = "949EAAFF";
			}
		}
		var herData = listdata.getOne(1);//第2个数据【他的】
		herData.communityList = communityList;
		listdata.updateOne(1, herData);
		//listview.refreshItems();
	}
}

//重画【他的】ui
function reDrawHerInfo(){
	if(listdata.getCount()<2){
		return;
	}
	var herData = listdata.getOne(1);//第2个数据【他的】
	if(curTabIndex==1){//他的
		herData.showInrerests = true;//显示爱好
		herData.showIndustry = true;
		herData.showHomeTown = true;
		herData.showDelicacy = true;
		herData.showGraduateSchool = true;
		herData.showEntertainment = true;
		herData.showCommunity = true;//显示社区layout
		herData.showJoinCommunity = true;//显示社区label
		herData.showJoinCommunityCount = true;//显示社区数量
		herData.showCircle = true;//显示圈子
		herData.showShop = true;//显示店铺
		
		herData.showJoinCommunity2 = false;//显示社区【长】
		herData.showHerCircle = false;//显示-他的圈子label
		listdata.updateOne(1, herData);
		listview.refreshItems();//刷新item数据
	}else if(curTabIndex == 2){//社区
		herData.showInrerests = false;//显示爱好
		herData.showIndustry = false;
		herData.showHomeTown = false;
		herData.showDelicacy = false;
		herData.showGraduateSchool = false;
		herData.showEntertainment = false;
		
		herData.showJoinCommunity = false;//显示社区
		herData.showJoinCommunityCount = false;//显示社区数量
		herData.showCircle = false;//显示圈子
		herData.showShop = false;//显示店铺

		herData.showCommunity = true;//显示社区layout
		herData.showJoinCommunity2 = true;//显示社区【长】
		herData.showHerCircle = false;//显示-他的圈子label
		listdata.updateOne(1, herData);
		listview.refreshItems();//刷新item数据
	}else if(curTabIndex == 3){//圈子
		herData.showInrerests = false;//显示爱好
		herData.showIndustry = false;
		herData.showHomeTown = false;
		herData.showDelicacy = false;
		herData.showGraduateSchool = false;
		herData.showEntertainment = false;
		herData.showCommunity = false;//显示社区layout
		herData.showCircle = false;//显示圈子
		herData.showShop = false;//显示店铺

		herData.showHerCircle = true;//显示-他的圈子label
		listdata.updateOne(1, herData);
		listview.refreshItems();//刷新item数据
	}
	
}

//加为好友
function addFriend(){
	friendHelper.addFriendApply(userId,function(data){
		nf.toast("添加好友申请已发送");
	});
}
//删除好友
function delFriend(){
	nf.confirm("确定要删除这个好友吗?", "", "确定", "取消", function(data, e) {
		if (data == 1) {
			friendHelper.deleteFriend(userId, function(data) {
				isFriend = false;
				reDrawFooter();
				nf.toast("删除好友成功");
				imHelper.removeHistoryMessage(userId);
			});
		}
	})
}

//右上角menu--begin
var menus= [];
menus=[{"template" : 0,"menuIcon":"source://image/menu/userdel.png","menuName":"删除好友","type":"delFriend"}];
var popmenu = ui("$").add("menupop", "source://view/reuse/menu/menul.ui", 0, 0);
var popmenuf = ui("menupop");
page.on("menusSHow", function() {
	//nf.toast("点击--右上角图标")
	popmenuf.fire("popmenuSJ", menus);
});
popmenuf.on("popmenuItem", function(data) {
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	if(actionType=="addFriend"){
		popmenuf.fire("popmenuClose");
		addFriend();
	}else if(actionType=="delFriend"){
		popmenuf.fire("popmenuClose");
		delFriend();
	}
});
//右上角menu--end

//改变右上角--菜单项
function isFriendMenu(flag) {
	if(flag){
		menus=[{"template" : 0,"menuIcon":"source://image/menu/userdel.png","menuName":"删除好友","type":"delFriend"}];
	}/*else{
		menus=[{"template" : 0,"menuIcon":"source://image/menu/useradd.png","menuName":"加为好友","type":"addFriend"}];
	}*/
}